home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.7z / ftp.whtech.com / emulators / mess / MESS 0.133 / Macintosh / MessMenu 0.7.0 OSX uni.dmg / MessMenu.app / Contents / MacOS / filesel.tcl < prev    next >
Encoding:
Text File  |  2009-01-01  |  9.7 KB  |  343 lines

  1. # fileselect.tcl --
  2. # simple file selector.
  3. #
  4. # Mario Jorge Silva                      msilva@cs.Berkeley.EDU
  5. # University of California Berkeley                 Ph:    +1(510)642-8248
  6. # Computer Science Division, 571 Evans Hall         Fax:   +1(510)642-5775
  7. # Berkeley CA 94720                                 
  8.  
  9. # Layout:
  10. #
  11. #  file:                  +----+
  12. #  ____________________   | OK |
  13. #                         +----+
  14. #
  15. #  +------------------+    Cancel
  16. #  | ..               |S
  17. #  | file1            |c
  18. #  | file2            |r
  19. #  |                  |b
  20. #  | filen            |a
  21. #  |                  |r
  22. #  +------------------+
  23. #  currrent-directory
  24. #
  25. # Copyright 1993 Regents of the University of California
  26. # Permission to use, copy, modify, and distribute this
  27. # software and its documentation for any purpose and without
  28. # fee is hereby granted, provided that this copyright
  29. # notice appears in all copies.  The University of California
  30. # makes no representations about the suitability of this
  31. # software for any purpose.  It is provided "as is" without
  32. # express or implied warranty.
  33. #
  34.  
  35. # Copyright 1996
  36.  
  37. # Slight modifications to and adoption to Tk4.0 were made to this
  38. # fileselectionbox code by Lakshmi Sastry, Rutherford Appleton Laboratory,
  39. # chilton, Didcot, OXON, OX11 0QX, UK.
  40.  
  41. # You can now type in a non-existing file name as well. This file name is
  42. # returned for the application to open a new file to write to 
  43.  
  44. # AGOCG Tcl/Tk Cookbook
  45. # Authors
  46.  
  47. # Lakshmi Sastry
  48. # Computing and Information Systems Department
  49. # Rutherford Appleton Laboratory, Chilton, Didcot. OX11 0QX
  50. # lakshmi.sastry@rl.ac.uk
  51.  
  52. #                         and
  53.  
  54. # Venkat VSS Sastry
  55. # Department of Applied Mathematics and Operational Research
  56. # Cranfield University, RMCS Shrivenham, Swindon, SN6 8LA
  57. # sastry@rmcs.cran.ac.uk
  58.  
  59. # Permission to use, copy, modify, and distribute this
  60. # software and its documentation for any purpose and without
  61. # fee is hereby granted, provided that this copyright
  62. # notice appears in all copies.
  63.   
  64. # The authors, RAL, RMCS Shrivenham, Cranfield University and AGOCG
  65. # make no representations about the suitability of this
  66. # software for any purpose.  It is provided "as is" without
  67. # express or implied warranty. Likewise they accept no responsibility
  68. # whatsoever for any public domain software modules used (which are
  69. # hereby acknowledged) in this software 
  70.  
  71.  
  72. # names starting with "fileselect" are reserved by this module
  73. # no other names used.
  74.  
  75. # use the "option" command for further configuration
  76.  
  77. option add *Listbox*font \
  78.     "-*-helvetica-medium-r-normal-*-12-*-*-*-p-*-iso8859-1" startupFile
  79. option add *Entry*font \
  80.     "-*-helvetica-medium-r-normal-*-12-*-*-*-p-*-iso8859-1" startupFile
  81. option add *Label*font \
  82.     "-*-helvetica-medium-r-normal-*-12-*-*-*-p-*-iso8859-1" startupFile
  83.  
  84. set lscmd "/bin/ls | sed 's/\\\\/\\\\\\\\/g' | sed 's/\"/\\\\\\\\\\\\\"/g' | sed 's/^/\"/' | sed 's/$/\"/'"
  85. set device ""
  86.  
  87. # this is the default proc  called when "OK" is pressed
  88. # to indicate yours, give it as the first arg to "fileselect"
  89.  
  90. proc fileselect.default.cmd {f} {
  91.     global fileselect loadenable
  92.     set fileselect(selectedfile) $f
  93.     #puts stderr "selected file $f"
  94. }
  95.  
  96.  
  97. # this is the default proc called when error is detected
  98. # indicate your own pro as an argument to fileselect
  99.  
  100. proc fileselect.default.errorHandler {errorMessage} {
  101.     puts stdout "error: $errorMessage"
  102.     catch { cd ~ }
  103. }
  104.  
  105. # this is the proc that creates the file selector box
  106.  
  107. proc fileselect {
  108.     {cmd fileselect.default.cmd} 
  109.     {purpose "Select file:"} 
  110.     {w .fileSelectWindow} 
  111.     {errorHandler fileselect.default.errorHandler}} {
  112.  
  113.     catch {destroy $w}
  114.  
  115.     toplevel $w
  116.     grab $w
  117.     wm title $w "Select Disk or Cartrige File"
  118.     wm transient $w .
  119.     wm geometry $w +0+22
  120.  
  121.     # path independent names for the widgets
  122.     global fileselect
  123.     global selected
  124.     global lscmd
  125.     global device
  126.     global system
  127.     global appdir
  128.     set fileselect(devlist) $w.file.eframe.devlist
  129.     set fileselect(entry) $w.file.eframe.entry
  130.     set fileselect(list) $w.file.sframe.list 
  131.     set fileselect(scroll) $w.file.sframe.scroll
  132.     set fileselect(insert) $w.bframe.insert
  133.     set fileselect(done) $w.bframe.done
  134.     set fileselect(eject) $w.bframe.eject
  135.     set fileselect(dirlabel) $w.file.dirlabel
  136.  
  137.     # widgets
  138.     frame $w.file -bd 10 
  139.     frame $w.bframe -bd 10
  140.     pack append $w \
  141.         $w.file {left filly} \
  142.         $w.bframe {left expand frame n}
  143.  
  144.     frame $w.file.eframe
  145.     frame $w.file.sframe
  146.     label $w.file.dirlabel -anchor e -width 24 -text [pwd] 
  147.  
  148.     pack append $w.file \
  149.         $w.file.eframe {top frame w} \
  150.     $w.file.sframe {top fillx} \
  151.     $w.file.dirlabel {top frame w}
  152.  
  153.     spinbox $w.file.eframe.devlist -textvariable device
  154.     set fl [open "| \"$appdir/listdevs.sh\" $system"]
  155.     set values [list]
  156.     while {[eof $fl] == 0} {
  157.     gets $fl dev
  158.     if {[eof $fl] == 0} {
  159.         lappend values $dev
  160.     }
  161.     }
  162.     if {[catch {close $fl} err]} {
  163.         puts "Failed to read $system device list: $err"
  164.     }
  165.     $w.file.eframe.devlist configure -values $values
  166.     label $w.file.eframe.label -anchor w -width 24 -text $purpose
  167.     entry $w.file.eframe.entry -relief sunken 
  168.  
  169.     pack append $w.file.eframe \
  170.         $w.file.eframe.devlist {top} \
  171.         $w.file.eframe.label {top expand frame w} \
  172.                 $w.file.eframe.entry {top fillx frame w} 
  173.  
  174.  
  175.     scrollbar $w.file.sframe.yscroll -relief sunken \
  176.      -command "$w.file.sframe.list yview"
  177.     listbox $w.file.sframe.list -relief sunken -selectmode single \
  178.     -yscroll "$w.file.sframe.yscroll set" 
  179.     #$fileselect(list) configure -selectmode single
  180.     pack append $w.file.sframe \
  181.         $w.file.sframe.yscroll {right filly} \
  182.      $w.file.sframe.list {left expand fill} 
  183.  
  184.     # buttons
  185.     button $w.bframe.insert -text Insert -relief raised -padx 10 \
  186.         -command "fileselect.insert.cmd $w $cmd $errorHandler"
  187.  
  188.     button $w.bframe.done -text Done -relief raised -padx 10 \
  189.         -command "fileselect.done.cmd $w"
  190.     button $w.bframe.eject -text Eject -relief raised -padx 10 \
  191.         -command "fileselect.eject.cmd $w"
  192.  
  193.     pack append $w.bframe $w.bframe.insert {top}\
  194.                           $w.bframe.eject {top} $w.bframe.done {top}
  195.  
  196.     # Fill the listbox with a list of the files in the directory (run
  197.     # the "/bin/ls" command to get that information).
  198.     # to not display the "." files, remove the -a option and fileselect
  199.     # will still work
  200.  
  201.     $fileselect(list) insert end ".."
  202.     foreach i [exec /bin/sh -c $lscmd] {
  203.         $fileselect(list) insert end $i
  204.     }
  205.  
  206.    # Set up bindings for the browser.
  207.     bind $fileselect(entry) <Return> {eval $fileselect(insert) invoke} 
  208.     bind $fileselect(entry) <Control-c> {eval $fileselect(done) invoke}
  209.  
  210.     bind $fileselect(list) <Button-1> {
  211.         # puts stderr "button 1 release"
  212.     set x [$fileselect(list) curselection]
  213.     $fileselect(entry) delete 0 end
  214.     $fileselect(entry) insert 0 [%W get [%W nearest %y]]
  215.     }
  216.  
  217.     bind $fileselect(list) <Key> {
  218.     set x [$fileselect(list) curselection]
  219.         $fileselect(entry) delete 0 end
  220.     $fileselect(entry) insert 0 [%W get [%W nearest %y]]
  221.     }
  222.  
  223.     bind $fileselect(list) <Double-ButtonPress-1> {
  224.         # puts stderr "double button 1"
  225.            set x [$fileselect(list) curselection]
  226.     $fileselect(entry) delete 0 end
  227.     $fileselect(entry) insert 0 [%W get [%W nearest %y]]
  228.     $fileselect(insert) invoke
  229.     }
  230.  
  231.     bind $fileselect(list) <Return> {
  232.     set x [$fileselect(list) curselection]
  233.     $fileselect(entry) delete 0 end
  234.     $fileselect(entry) insert 0 [%W get [%W nearest %y]]
  235.     $fileselect(insert) invoke
  236.     }
  237.  
  238.     # set kbd focus to entry widget
  239.  
  240.     focus $fileselect(entry)
  241.  
  242. }
  243.  
  244.  
  245. # auxiliary button procedures
  246.  
  247. proc fileselect.done.cmd {w} {
  248.     # puts stderr "Done"
  249.     global fileselect loadenable
  250.     set fileselect(selectedfile) ""
  251.     set loadenable 0
  252.     destroy $w
  253. }
  254.  
  255. proc fileselect.eject.cmd {w} {
  256.     # puts stderr "Eject"
  257.     global fileselect loadenable
  258.     set fileselect(selectedfile) ":UNLOAD:"
  259.     destroy $w
  260. }
  261.  
  262. proc fileselect.insert.cmd {w cmd errorHandler} {
  263.     global fileselect
  264.     global selected
  265.     global lscmd
  266.     set selected [$fileselect(entry) get]
  267.         # some nasty file names may cause "file isdirectory" to return an error
  268.     set sts [catch { 
  269.     file isdirectory $selected
  270.     }  errorMessage ]
  271.  
  272.     if { $sts != 0 } then {
  273.     $errorHandler $errorMessage
  274.     destroy $w
  275.     return
  276.  
  277.     }
  278.  
  279.     # clean the text entry and prepare the list
  280.       $fileselect(entry) delete 0 end
  281.       $fileselect(list) delete 0 end
  282.       $fileselect(list) insert end ".."
  283.  
  284.     # perform globbing on the selection. 
  285.     # If globing returns an error (no match) check if a non-null name is
  286.     # entered. If name string is non-empty return it as a new file name
  287.     # else give an error message.
  288.     # If resulting list length > 1, put the list on the file listbox and return
  289.     # If globing expands to a list of filenames in multiple directories,
  290.     # the indicated regexp is invalid and the error handler is called instead.
  291.     set globlist 0
  292.  
  293.     set sts [catch {
  294.     set globlist [glob [list $selected]]
  295.     } errorMessage ]
  296.  
  297.     if { $sts != 0 } then {
  298.     if { [llength $globlist] == 1 } {
  299.         destroy $w
  300.         $cmd $selected
  301.         return 
  302.     } else {
  303.         $errorHandler $errorMessage
  304.         destroy $w
  305.         return
  306.        }
  307.  
  308.     
  309.     } 
  310.  
  311.     if {[llength $globlist] > 1} {
  312.     if {[regexp "/" $globlist] != 0} {
  313.         $errorHandler [list "Invalid regular expression, " $selected, "."]
  314.         destroy $w
  315.         return
  316.     }
  317.     foreach i $globlist {
  318.         if {[string compare $i "."] != 0 && \
  319.         [string compare $i ".."] != 0} {
  320.         $fileselect(list) insert end $i
  321.         }
  322.     }
  323.     return
  324.     }
  325.  
  326.     # selection may be a directory. Expand it.
  327.  
  328.     if {[file isdirectory $selected] != 0} {
  329.     cd $selected
  330.     set dir [pwd]
  331.     $fileselect(dirlabel) configure -text $dir
  332.     foreach i [exec /bin/sh -c $lscmd] {
  333.         $fileselect(list) insert end $i
  334.     }
  335.     return
  336.     }
  337.  
  338.     destroy $w
  339.     $cmd $selected
  340.     
  341. }
  342.